//
//  PesonalInformationViewController.m
//  AddressBook
//
//  Created by mac on 2017/8/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "PesonalInformationViewController.h"
#import "ChangeDataViewController.h"

@interface PesonalInformationViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *resourceData;
@property (nonatomic, strong) UILabel *addPhone;
@property (nonatomic, strong) UILabel *nameLable;
@property (nonatomic, strong) UILabel *idCardLable;
@property (nonatomic, strong) NSDictionary *userData;
@property (nonatomic, strong) UserDataModel *model;

@end

@implementation PesonalInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.resourceData = [NSMutableArray array];
    [self.view addSubview:self.tableView];
//    self.userData = [[NSUserDefaults standardUserDefaults] objectForKey:KUSERINFORMATION];
    self.model = [UserDataModel getLocalModel];
    //self.resourceData = self.model.userinfo[@"phone_name"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOrIdCard:) name:@"personalData" object:@"data"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOrIdCards:) name:@"personalDataID" object:@"data"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPhoneNotification:) name:@"AddPhone" object:@"phone"];
}

-(void)nameOrIdCard:(NSNotification *)notification{
    
    self.nameLable.text = notification.userInfo[@"formason"];
}

-(void)nameOrIdCards:(NSNotification *)notification{
    
    self.idCardLable.text = notification.userInfo[@"formason"];
}


-(void)addPhoneNotification:(NSNotification *)notification{
    //NSArray *arr = [self.model.userinfo[@"phone_name"] arrayByAddingObject:notification.userInfo];
    //self.resourceData = [arr mutableCopy];
    [self.tableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3+self.resourceData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellStr = @"informationCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"姓名";
        [cell.contentView addSubview:self.nameLable];
        //self.nameLable.text = self.model.userinfo[@"member_name"];
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"身份证";
        [cell.contentView addSubview:self.idCardLable];
        //self.idCardLable.text = self.model.userinfo[@"id_card"];
    }else if (indexPath.row == 2){
//        cell.textLabel.textAlignment = NSTextAlignmentCenter;
//        cell.textLabel.text = @"添加电话";
        [cell.contentView addSubview:self.addPhone];
    }else{
        cell.textLabel.text = self.resourceData[indexPath.row - 3][@"phone"];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hidesBottomBarWhenPushed = YES;
    ChangeDataViewController *changeVC = [[ChangeDataViewController alloc]init];
    if (indexPath.row == 0) {
        changeVC.titleText = @"姓名";
        [self.navigationController pushViewController:changeVC animated:YES];
    }else if (indexPath.row == 1){
        changeVC.titleText = @"身份证";
        [self.navigationController pushViewController:changeVC animated:YES];
    }else if (indexPath.row == 2){
        changeVC.titleText = @"添加电话";
        [self.navigationController pushViewController:changeVC animated:YES];
    }else{
        
    }
    
    
//    self.hidesBottomBarWhenPushed = NO;
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        //        _tableView.tableHeaderView = self.hearView;
    }
    return _tableView;
}

-(UILabel *)addPhone{
    if (!_addPhone) {
        _addPhone = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 44)];
        _addPhone.text = @"添加号码";
        _addPhone.textAlignment = NSTextAlignmentCenter;
        
    }
    return _addPhone;
}

-(UILabel *)nameLable{
    if (!_nameLable) {
        _nameLable = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW-205*ScaleW, 0, 200*ScaleW, 44)];
        _nameLable.textAlignment = NSTextAlignmentRight;
        _nameLable.textColor = [UIColor grayColor];
        
    }
    return _nameLable;
}

-(UILabel *)idCardLable{
    if (!_idCardLable) {
        _idCardLable = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW - 305, 0, 300, 44)];
        _idCardLable.textAlignment = NSTextAlignmentRight;
        _idCardLable.textColor = [UIColor grayColor];
        
    }
    return _idCardLable;
}

@end
